package com.glory.wxelectricitypda.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.bean.FTDataAcqQueryBean;

import java.text.DecimalFormat;
import java.util.List;

public class FTReportMESAdapter extends BaseQuickAdapter<FTDataAcqQueryBean.HbinEDClistDTO,BaseViewHolder> {


    public FTReportMESAdapter(@Nullable List<FTDataAcqQueryBean.HbinEDClistDTO> data) {
        super(R.layout.adapter_ft_data_acq_bin,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FTDataAcqQueryBean.HbinEDClistDTO item) {

        helper.setText(R.id.binName,item.getBinName());
        helper.setText(R.id.binReak,item.getBinReak());
        helper.setText(R.id.binQty,String.valueOf(item.getBinQty()));
        helper.setText(R.id.binYie,item.getBinYie());
        EditText editText = helper.getView(R.id.binQty);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                int binqty = 0;
                if (editable.toString().isEmpty()){
                    item.setBinQty(0);
                    binqty = 0;
                }else {
                    item.setBinQty(Integer.parseInt(editable.toString()));
                    binqty = Integer.parseInt(editable.toString());
                }
                int num = 0;
                for (FTDataAcqQueryBean.HbinEDClistDTO bean: getData()) {
                    int num01 = bean.getBinQty();
                    num = num+num01;
                }
                if (binqty == 0){
                    helper.setText(R.id.binYie,"0.00%");
                    item.setBinYie("0.00%");
                    return;
                }
                double num02 = Double.valueOf(binqty)/num;
                String formatted = new DecimalFormat("0.00%").format(num02);
                helper.setText(R.id.binYie,formatted);
                item.setBinYie(formatted);
            }
        });

    }
}
